package com.victor.pomelo.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.shiro.authz.permission.WildcardPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.victor.pomelo.dao.MenuMapper;
import com.victor.pomelo.dao.ResourceMapper;
import com.victor.pomelo.dao.UserMapper;
import com.victor.pomelo.model.Menu;
import com.victor.pomelo.model.Pager;
import com.victor.pomelo.model.Resource;
import com.victor.pomelo.model.User;
import com.victor.pomelo.service.UserService;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;

	@Autowired
	private ResourceMapper resourceMapper;

	@Autowired
	private MenuMapper menuMapper;

	@Override
	public User findByUsername(String username) {
		User user = userMapper.findByUsername(username);
		return user;
	}

	@Override
	public User createUser(User user) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public User updateUser(User user) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void deleteUser(Long userId) {
		// TODO Auto-generated method stub

	}

	@Override
	public void changePassword(Long userId, String newPassword) {
		// TODO Auto-generated method stub

	}

	@Override
	public User findById(Long userId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<User> findAll() {
		return userMapper.findAll();
	}

	@Override
	public List<User> findAll1(Long start, Long offset) {
		return userMapper.findAll1(start, offset);

	}

	@Override
	public Set<String> findRoles(String username) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 通过用户名称查询用户对应所有权限
	 */
	@Override
	public Set<String> findPermissions(String username) {
		Set<String> permissisons = new HashSet<String>();
		User user = userMapper.findByUsername(username);
		permissisons = userMapper.findPermissionById(user.getUserid());
		return permissisons;
	}

	/**
	 * 通过用户名查询用户名可以使用的所有菜单子菜单列表
	 */
	@Override
	public List<Resource> findResources(Set<String> permissions) {

		List<Resource> ownerResources = new ArrayList<Resource>();

		List<Resource> allResources = resourceMapper.findAll();
		for (Resource resource : allResources) {
			if (hasPermission(permissions, resource)) {
				ownerResources.add(resource);
			}
		}

		return ownerResources;
	}

	private boolean hasPermission(Set<String> permissions, Resource resource) {
		if (StringUtils.isEmpty(resource.getPermission())) {
			return true;
		}
		for (String permission : permissions) {
			WildcardPermission p1 = new WildcardPermission(permission);
			WildcardPermission p2 = new WildcardPermission(
					resource.getPermission());
			if (p1.implies(p2) || p2.implies(p1)) {
				return true;
			}
		}
		return false;
	}

	@Override
	public List<Menu> findMenus(Map<Long, Resource> resources) {
		List<Menu> allMenus = menuMapper.findAll(); 
		return null;
	}

	public Pager<User> findByGroupId(Long groupid, Pager page) {
		Pager pager = new Pager<User>();
		pager.setPage(1);
		pager.setRecords(userMapper.findCountByGroupid(groupid));
//		pager.setRowNum(rowNum);
//		userMapper.findByGroupid(groupid, start, offset);
		return null;
	}
}
